package org.ws.task.netty.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class MessageEncoder extends MessageToByteEncoder<Object> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object o, ByteBuf out) throws Exception {
        byte[] bytes = CodecUtil.encode(o);

        int type = CodecUtil.getType(o);
        int dataLength = bytes.length;
        out.writeInt(4 + dataLength);
        out.writeInt(type);
        out.writeBytes(bytes);
    }
}
